package com.jmhy.sdk.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jmhy.sdk.common.JiMiSDK;
import com.jmhy.sdk.config.AppConfig;
import com.jmhy.sdk.http.ApiAsyncTask;
import com.jmhy.sdk.http.ApiRequestListener;
import com.jmhy.sdk.model.Guest;
import com.jmhy.sdk.model.LoginMessage;
import com.jmhy.sdk.sdk.JmhyApi;
import com.jmhy.sdk.utils.FragmentUtils;
import com.jmhy.sdk.utils.Seference;
import com.jmhy.sdk.utils.Utils;

public class JmUserSelectLoginFragment8 extends JmBaseFragment implements View.OnClickListener {

    private static final int ACTION_BACK = 1000;
    private static final int ACTION_LOGIN = 1001;
    private static final int ACTION_MOBILE_LOGIN = 1002;
    private static final int ACTION_QUICK_LOGIN = 1003;
    private static final int ACTION_KEFU = 1004;

    private View view;
    private ImageView ivReback, ivLogin, ivMobileLogin, ivQuickLogin;
    private TextView tvKefu, tvTitle;
    private ApiAsyncTask mGuestTask;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(AppConfig.resourceId(getActivity(), "jm_select_login_8", "layout"), container, false);
            view.setClickable(true);
            initView(view);
        }
        return view;
    }

    private void initView(View view) {

        tvTitle = view.findViewById(AppConfig.resourceId(getActivity(), "tv_title", "id"));
        tvTitle.setText(AppConfig.appName);

        ivReback = view.findViewById(AppConfig.resourceId(getActivity(), "btn_reback", "id"));
        ivReback.setTag(ACTION_BACK);
        ivReback.setOnClickListener(this);


        Seference seference = new Seference(getActivity());

        if (seference.isExitData()) {
            ivReback.setVisibility(View.VISIBLE);
        }


        ivLogin = view.findViewById(AppConfig.resourceId(getActivity(), "btn_login", "id"));
        ivLogin.setTag(ACTION_LOGIN);
        ivLogin.setOnClickListener(this);

        ivMobileLogin = view.findViewById(AppConfig.resourceId(getActivity(), "btn_mobile_login", "id"));
        ivMobileLogin.setTag(ACTION_MOBILE_LOGIN);
        ivMobileLogin.setOnClickListener(this);

        ivQuickLogin = view.findViewById(AppConfig.resourceId(getActivity(), "btn_quick_login", "id"));
        ivQuickLogin.setTag(ACTION_QUICK_LOGIN);
        ivQuickLogin.setOnClickListener(this);

        tvKefu = view.findViewById(AppConfig.resourceId(getActivity(), "tv_kefu", "id"));
        tvKefu.setTag(ACTION_KEFU);
        tvKefu.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch ((Integer) v.getTag()) {
            case ACTION_BACK:
                Fragment selectAccountLoginFragment = FragmentUtils.getJmSkin8Fragment(getActivity(), FragmentUtils.SDK_SELECT_ACCOUNT_TAG);
                replaceFragmentToActivity(getFragmentManager(), selectAccountLoginFragment, AppConfig.resourceId(getActivity(), "content", "id"));
                break;
            case ACTION_LOGIN:
                Fragment mUserLoginFragment = FragmentUtils.getJmSkin8Fragment(getActivity(), FragmentUtils.SDK_LOGIN_TAG);
                replaceFragmentToActivity(getFragmentManager(), mUserLoginFragment, AppConfig.resourceId(getActivity(), "content", "id"));
                break;
            case ACTION_MOBILE_LOGIN:
                Fragment mobileLoginFragment = FragmentUtils.getJmSkin8Fragment(getActivity(), FragmentUtils.SDK_MOBILE_LOGIN_TAG);
                replaceFragmentToActivity(getFragmentManager(), mobileLoginFragment, AppConfig.resourceId(getActivity(), "content", "id"));
                break;
            case ACTION_QUICK_LOGIN:
                getGuest();
                break;
            case ACTION_KEFU:
                turnToIntent(AppConfig.KEFU);
                break;
        }

    }


    /**
     * 游客登录
     */
    public void getGuest() {
        mGuestTask = JmhyApi.get().starguestLogin(getActivity(),
                AppConfig.appKey, new ApiRequestListener() {
                    @Override
                    public void onSuccess(Object obj) {
                        // TODO Auto-generated method stub
                        if (obj != null) {
                            Guest guest = (Guest) obj;
//							if (!guest.getUpass().equals("")||guest.getIs_package_new().equals("1")){
                            if (!guest.getUpass().equals("")) {
                                JiMiSDK.getStatisticsSDK().onRegister("JiMiSDK", true);
                            }
                            if (guest.getCode().equals("0")) {
                                mSeference.saveAccount(guest.getUname(), "~~test", guest.getLogin_token());
                                AppConfig.saveMap(guest.getUname(), "~~test", guest.getLogin_token());
                                Utils.saveUserToSd(getActivity());
                                sendData(AppConfig.GUEST_lOGIN_SUCCESS, obj, handler);
                            } else {
                                sendData(AppConfig.FLAG_FAIL, guest.getMessage(), handler);
                                JiMiSDK.reportRequestErrorAction(getActivity(), "sdk_guest_login_error", guest.getCode(), guest.getMessage());

                            }
                        } else {
                            sendData(AppConfig.FLAG_FAIL, AppConfig.getString(getActivity(), "http_rror_msg"), handler);
                        }
                    }

                    @Override
                    public void onError(int statusCode) {
                        // TODO Auto-generated method stub
                        sendData(AppConfig.FLAG_FAIL, AppConfig.getString(getActivity(), "http_rror_msg"), handler);
                    }
                });

    }

    @Override
    public void onDestroy() {
        if (mGuestTask != null) {
            mGuestTask.cancel(false);
        }
        super.onDestroy();
    }


    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            if (getActivity() == null || getActivity().isFinishing()) {
                return;
            }
            switch (msg.what) {
                case AppConfig.FLAG_FAIL:
                    String resultmsg = (String) msg.obj;
                    showMsg(resultmsg);
                    break;

                case AppConfig.LOGIN_SUCCESS:
                    LoginMessage result = (LoginMessage) msg.obj;
                    showUserMsg(result.getUname());
                    AppConfig.USERURL = Utils.toBase64url(result.getFloat_url_user_center());

                    String url = Utils.toBase64url(result.getShow_url_after_login());
                    if (TextUtils.isEmpty(url)) {
                        url = Utils.toBase64url(result.getShow_url_popup());
                    }
                    turnToNotice(url);
                    getActivity().finish();
                    break;
                case AppConfig.FLAG_SHOW_POPWINDOW:
                    break;
                case AppConfig.GUEST_lOGIN_SUCCESS:
                    Guest guest = (Guest) msg.obj;
                    String murl = Utils.toBase64url(guest.getShow_url_after_login());
                    if (TextUtils.isEmpty(murl)) {
                        murl = Utils.toBase64url(guest.getShow_url_popup());
                    }
                    if (!TextUtils.isEmpty(guest.getUpass())) {
                        Bundle args = new Bundle();
                        // Log.i("kk",mobileUser.getMoblie())
                        args.putString("username", guest.getUname());
                        args.putString("upass", guest.getUpass());
                        args.putString("msg", guest.getMessage());
                        args.putString("gametoken", guest.getGame_token());
                        args.putString("openid", guest.getOpenid());
                        args.putString("url", murl);
                        Fragment mJmSetUserFragment = FragmentUtils.getJmSetUserFragment(getActivity(), args);
                        replaceFragmentToActivity(getFragmentManager(), mJmSetUserFragment, AppConfig.resourceId(getActivity(), "content", "id"));
                    } else {
                        wrapaLoginInfo("success", guest.getMessage(), guest.getUname(), guest.getOpenid(), guest.getGame_token());
                        turnToNotice(murl);
                        getActivity().finish();
                    }
                    break;
            }
        }
    };
}
